/*    */ package com.newcrm.webapi.sub;
/*    */ 
/*    */ import com.newcrm.bean.User;
/*    */ import com.newcrm.db.UserManager;
/*    */ import com.newcrm.exception.NotFoundException;
/*    */ import com.newcrm.util.Util;
/*    */ import com.newcrm.webapi.AbstractAction;
/*    */ import com.newcrm.webapi.AbstractHttpServlet;
/*    */ import java.io.File;
/*    */ import java.io.IOException;
/*    */ import java.util.Iterator;
/*    */ import java.util.List;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import org.apache.commons.fileupload.FileItem;
/*    */ import org.apache.commons.fileupload.FileUploadException;
/*    */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*    */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*    */ import org.apache.log4j.Logger;
/*    */ 
/*    */ public class UpdateAppUserAvatar extends AbstractAction
/*    */ {
/* 28 */   private static Logger logger = Logger.getLogger(UpdateAppUserAvatar.class
/* 29 */     .getName());
/*    */ 
/* 31 */   private String path = "";
/*    */ 
/*    */   public UpdateAppUserAvatar(AbstractHttpServlet servlet) {
/* 34 */     super(servlet);
/*    */   }
/*    */ 
/*    */   public UpdateAppUserAvatar(AbstractHttpServlet servlet, String uploadPath) {
/* 38 */     super(servlet);
/* 39 */     this.path = uploadPath;
/*    */   }
/*    */ 
/*    */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*    */     throws IOException, ServletException, NotFoundException
/*    */   {
/* 46 */     User loginUser = super.getLoginUser(request);
/*    */ 
/* 48 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/* 49 */     ServletFileUpload upload = new ServletFileUpload(fac);
/* 50 */     upload.setHeaderEncoding("utf-8");
            List fileList;
/*    */     try
/*    */     {
/* 53 */       fileList = upload.parseRequest(request);
/*    */     }
/*    */     catch (FileUploadException e1)
/*    */     {
/*    */
/* 55 */       e1.printStackTrace();
/*    */       return;
/*    */     }
/* 59 */     Iterator it = fileList.iterator();
/*    */ 
/* 61 */     String imgPath = "";
/* 62 */     while (it.hasNext()) {
/* 63 */       FileItem item = (FileItem)it.next();
/* 64 */       if (!item.isFormField())
/*    */       {
/* 68 */         String fileName = item.getName();
/* 69 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*    */         {
/* 73 */           String extName = "";
/* 74 */           if (fileName.lastIndexOf(".") >= 0) {
/* 75 */             extName = fileName.substring(fileName.lastIndexOf("."));
/*    */           }
/*    */ 
/* 78 */           if (!Util.isImageFile(extName, item.get())) {
/* 79 */             logger.warn("file is not image " + fileName);
/* 80 */             super.showErrorMessage(response, "您上传的文件格式不正确，请上传图片文件");
/* 81 */             return;
/*    */           }
/* 83 */           extName = extName.toLowerCase();
/*    */ 
/* 85 */           imgPath = loginUser.userId + extName;
/*    */ 
/* 87 */           File file = new File(this.path, imgPath);
/*    */           try {
/* 89 */             item.write(file);
/*    */           } catch (Exception e) {
/* 91 */             logger.warn("save image file failed", e);
/*    */           }
/*    */         }
/*    */       }
/*    */     }
/* 94 */     UserManager manager = new UserManager();
/*    */     try {
/* 96 */       int result = manager.updateAvatar(loginUser.userId, imgPath);
/* 97 */       if (result <= 0) {
/* 98 */         super.showErrorMessage(response, "用户头像更新失败，请稍后再试");
/* 99 */         return;
/*    */       }
/*    */ 
/* 102 */       super.showSuccessMessage(response);
/*    */     } catch (Exception e) {
/* 104 */       logger.warn("update avatar failed", e);
/* 105 */       super.showErrorMessage(response, "用户头像更新失败，请稍后再试");
/* 106 */       return;
/*    */     }
/*    */   }
/*    */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.UpdateAppUserAvatar
 * JD-Core Version:    0.6.1
 */